<template>
  <div class="studentName">
    <div class="left-view">
      <div v-if="!list[leftIndex].imageUrl" class="image-mask">添加图片</div>
      <!--有图片-->
      <div v-else class="haveImg">
        <img :src="list[leftIndex].imageUrl" alt />
        <a :href="list[leftIndex].link" />
      </div>
    </div>
    <div v-if="list[leftIndex].showRight" class="right-view">
      <div class="nav-title">图片</div>
      <div class="form-title">上传图片</div>
      <div class="uplode-box">
        <el-upload
          :show-file-list="false"
          :on-success="handleNavImgSuccess"
          :before-upload="beforeNavImgUpload"
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
        >
          <img v-if="list[leftIndex].imageUrl" :src="list[leftIndex].imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <div class="hint">建议尺寸：690 * 420px 或 4:3, 小于5M, 支持jpg、png、jpeg格式</div>
      </div>
      <div class="form-title">跳转设置</div>
      <el-checkbox v-model="list[leftIndex].showLink" style="margin-bottom:5px;">点击图片跳转页面</el-checkbox>
      <el-input v-if="list[leftIndex].showLink" v-model="list[leftIndex].link" />
    </div>
  </div>
</template>

<script>
export default {
  props: ['list', 'leftIndex'],
  data() {
    return {

    }
  },
  methods: {
    handleNavImgSuccess(res, file) {
      this.list[this.leftIndex].imageUrl = URL.createObjectURL(file.raw)
    },
    beforeNavImgUpload(file) {
      const isJPG = file.type === 'image/jpeg' || 'image/png'
      const isLt5M = file.size / 1024 / 1024 < 5

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt5M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return isJPG && isLt5M
    }
  }
}
</script>

<style lang="scss" scoped>
.studentName {
  padding: 10px;
  .left-view {
    .image-mask {
      width: 100%;
      height: 100%;
      background-color: #e1e1e1;
      font-size: 26px;
      color: #919191;
      font-weight: 700;
      text-align: center;
      line-height: 194px;
    }
    .haveImg {
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .right-view {
    position: absolute;
    top: 0;
    left: 100%;
    z-index: 7;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 355px;
    min-height: 28px;
    margin-left: 20px;
    padding: 0 20px 20px;
    color: #353535;
    font-size: 14px;
    font-family: 'PingFangSC-Medium';
    background: #f8f8f8;
    border: 1px solid #e5e5e5;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    .nav-title {
      height: 50px;
      font-weight: 800;
      line-height: 50px;
      border-bottom: 1px solid #eee;
    }
    .form-title {
      margin: 16px 0px;
      .sign {
        color: red;
      }
    }
    .uplode-box {
      display: flex;
      align-items: center;
      .avatar-uploader {
        .el-upload {
          border: 1px dashed #d9d9d9;
          border-radius: 6px;
          cursor: pointer;
          position: relative;
          overflow: hidden;
        }
        .avatar-uploader-icon {
          font-size: 28px;
          color: #8c939d;
          width: 100px;
          height: 70px;
          line-height: 70px;
          text-align: center;
          border: 1px solid #898989;
        }
        .avatar {
          width: 100px;
          height: 70px;
          display: block;
        }
      }
      .hint {
        height: 34px;
        line-height: 17px;
        font-size: 12px;
        color: #888;
        margin-left: 20px;
      }
    }
  }
  .right-view::before {
    border-color: transparent;
    border-right-color: #e5e5e5;
    position: absolute;
    top: 40px;
    left: -20px;
    content: ' ';
    width: 0px;
    height: 0px;
    border-width: 10px;
    border-style: solid;
  }
}
</style>